<template>
	<view>
		<view class="edgeInsetTop"></view>
		<form @submit="formSubmit">
			<view class="cu-list menu solid-bottom">
				<view class="cu-item arrow" style="min-height:150rpx;" @tap="Avatar">
					<view class="content">
						<text style="font-size: 30rpx;">门店头像</text>
					</view>
					<view class="action">
						<view class="cu-avatar round lg" :style="{backgroundImage: 'url('+ $wanlshop.oss(user.partner.image, 52, 52, 2, 'avatar') +')'}" ></view>
					</view>
				</view>
			</view>
			<view class="cu-form-group">
				<view class="title">门店名称</view>
				<input placeholder="请输入门店名称" name="store" :value="user.partner.store"></input>
			</view>
			<view class="cu-form-group area" @tap="btnClick">
			
				<view class="title">门店位置</view>
				<input type="text" :value="user.partner.address_city" name="address_city" placeholder="选择地区" disabled />
			</view>
			
			<view class="cu-form-group align-start">
				<view class="title">详细地址</view>
				<!-- 1.1.5升级 -->
				<textarea 
				name="address_detal"
				maxlength="-1" 
				@input="getInputtips"
				@focus="focusInputtips" 
				@confirm="confirmInputtips" 
				@blur="blurInputtips"
				:placeholder-style="addressData.adcode==''?'color: #bbb':''" 
				:adjust-position="false" 
				v-model="user.partner.address_detail"
				:disabled="addressData.adcode==''?true:false" 
				:placeholder="addressData.adcode==''?'还没有选择地区哦~ 选择地区后完成此项':'支持智能联想，输入小区名试试~'"/>
			</view>
			
			<scroll-view scroll-y="true" class="tipsScroll" v-show="!addressType">
				<view class="item" v-for="(item,index) in addressList.tips" :key="index" @tap="addressTisp(item)">					
					<text>{{item.name}}</text>
				</view>
			</scroll-view>
			
			<view class="cu-form-group">
				<view class="title">门店介绍</view>
				<input placeholder="请输入门介绍" name="remark" :value="user.partner.remark"></input>
			</view>

			<view class="padding-bj flex flex-direction">
				<button form-type="submit" class="cu-btn wanl-bg-redorange margin-tb lg">保存</button>				
			</view>		
			<wanl-address ref='wanlAddress' type='1' @selectAddress="successSelectAddress"></wanl-address> 
		</form>
           
	</view>
</template>

<script>
	import graceChecker from '@/common/graceChecker';
	import { mapState } from 'vuex';
	export default {
		computed: {
			...mapState(['user'])
		},
		data() {
			return {
				gender: true,
				genderdata: 0,

				addressData:{
					address_city:'',
					address_detail:'',		
					lat:'',
					lon:''
				},				
				addressList: {
					location: '',
					adcode: '',
					tips: {}
				},
				addressType: true
			}
		},
		onLoad() {
			//中央总线重新赋值Data
			console.log(this.user);
			this.gender = this.$store.state.user.gender == 0 ? true : false;
			this.genderdata = this.$store.state.user.gender;
		},
		methods: {
			btnClick() {
				this.$refs.wanlAddress.show()
			},
			successSelectAddress(address) {
				
			this.addressList.location = address.location;
			this.addressList.adcode = address.adcode;
			
			this.user.partner.address_city = address.province+'/'+address.city+'/'+address.district;
			
			},
			//调用高德地图搜索相关地址
			getInputtips(event) {
				let keywords = event.detail.value;
				if (keywords) {
					uni.request({
						url: 'https://restapi.amap.com/v3/assistant/inputtips',
						data: {
							key: this.$wanlshop.config('amapkey'),
							keywords: keywords,
							location: this.addressList.location,
							city: this.addressList.adcode
						},
						success: (res) => {
							console.log(res);
							let addressList = [];
							// 1.1.8升级
							if(res.data.infocode == 10001){
								this.$wanlshop.msg('获取位置失败：错误' + res.res.info);
							}else{
								res.data.tips.forEach(item => {
									if (item.address.length == 0) {
										item.address = '暂未收录地址';
										item.name = '暂未收录地址';
										item.location = '113.294701,22.666562';
									}
									addressList.push(item);
								});
							}
							this.addressList.tips = addressList;
						}
					});
				}
			},
			// 详细地址失去焦点
			blurInputtips(event) {
				this.addressData.address = event.detail.value;
				setTimeout(() => {
					this.addressType = true;
				}, 10);
			},
			// 详细地址已完成
			confirmInputtips(event) {
				this.addressType = true;
			},
			// 详细地址获取焦点
			focusInputtips(event) {
				this.addressType = false;
			},
			// 详细地址替换
			addressTisp(data) {
				console.log(data);
				this.addressData.adcode = data.adcode;
				this.addressData.address = data.address;
				
				this.user.partner.address_detail = data.name;
				
				this.addressData.lon = (data.location.split(','))['0'];
				this.addressData.lat = (data.location.split(','))['1'];
			
			},
			
			DateChange(e) {
				this.$store.state.user.birthday = e.detail.value
			},
			
			Gender(e) {
				if(e.detail.value == true){
					this.gender = true;
					this.genderdata = 0;
				}else{
					this.gender = false;
					this.genderdata = 1;
				}
			},
			Avatar(){
				this.$wanlshop.to('/pages/user2/partner/pimg');
			},
			formSubmit: function(e) {
				//定义表单规则	
				
				//进行表单检查
				let formData = e.detail.value;
				formData.lon = this.addressData.lon;
				formData.lat = this.addressData.lat;				
				
				// ..检查是否注册-没注册跳转注册
				if (checkRes) {
					uni.request({
						url: '/wanlshop/user/partneredit', 
						method: 'POST',
						data: formData,
						success: res => {
							// 保存用户信息
							this.$store.commit('user/setUserInfo', {partner: data.data});
							// 提示
							this.$wanlshop.msg('保存成功');
						}
					});
				} else {
					this.$wanlshop.msg(graceChecker.error);
				}
			}
		}
	}
</script>

<style>
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}
</style>
